/*
 * @author Guillermo Adrian Salguero Ortega
 */
package formula_urjc;

import clases.Carrera;
import clases.Circuito;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ModificarCarrera extends javax.swing.JFrame {

  private int indexCarrera; 
  
    public ModificarCarrera() {
        initComponents();
    }
     public ModificarCarrera(Carrera carreAux, int indexCarrera) {
        initComponents();
        // Inicializo las variables necesarias para buscar el circuito seleccionado
        int indiceSeleccionado = 0;
        int comboCount = this.jComboCircuitos.getItemCount();
        String circuito = carreAux.getCircuito();
        boolean exists = false;  
        //Busco cual circuito es el que tenia seleccionado inicialmente
        for (int index = 0; (index < comboCount) && (!exists); index++) {
            if(circuito.equalsIgnoreCase(this.jComboCircuitos.getItemAt(index).toString())){
              indiceSeleccionado=index;
              exists = true;
          }
         }
        this.tNombre.setText(carreAux.getNombre());
        this.jComboCircuitos.setSelectedIndex(indiceSeleccionado); 
        this.indexCarrera = indexCarrera;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        bCancelar = new javax.swing.JButton();
        bAceptar = new javax.swing.JButton();
        jComboCircuitos = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        tNombre = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Nombre");

        jLabel1.setText("Modificar Carrera");

        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        bAceptar.setText("Aceptar");
        bAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAceptarActionPerformed(evt);
            }
        });

        ArrayList<Circuito> listaCircuitos = Circuito.leerCircuito();
        String [] listString = new String[listaCircuitos.size()];
        int count = 0;
        for(Circuito circuitoAux : listaCircuitos) {
            String nombre = circuitoAux.getNombre();
            listString[count]=nombre;
            count = count + 1;
        }
        jComboCircuitos.setModel(new javax.swing.DefaultComboBoxModel(listString));
        jComboCircuitos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboCircuitosActionPerformed(evt);
            }
        });

        jLabel3.setText("Circuito");

        tNombre.setPreferredSize(new java.awt.Dimension(100, 20));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(13, 13, 13)
                                    .addComponent(bAceptar)
                                    .addGap(18, 18, 18))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addGap(65, 65, 65)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(65, 65, 65)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bCancelar)
                            .addComponent(jComboCircuitos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(175, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboCircuitos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(146, 146, 146)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bAceptar)
                            .addComponent(bCancelar)))
                    .addComponent(tNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        ListaEscuForm l = new ListaEscuForm();
        l.setVisible(true);

    }//GEN-LAST:event_bCancelarActionPerformed

    private void bAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAceptarActionPerformed
 
        //Recogemos los datos del formulario
            String nombre = tNombre.getText();
            String circuito = jComboCircuitos.getSelectedItem().toString();
        //Recupero la carrera original
         ArrayList<Carrera> listaCarreras = Carrera.leerCarrera();
            Carrera carreraNew = listaCarreras.get(indexCarrera);
            carreraNew.setNombre(nombre);
            carreraNew.setCircuito(circuito);
            listaCarreras.remove(indexCarrera);
            listaCarreras.add(carreraNew);
            Carrera.escribirCarrera(listaCarreras);            
            
            ListaCarrerasForm carreraForm = new ListaCarrerasForm();
            this.setVisible(false);
            carreraForm.setVisible(true);

    }//GEN-LAST:event_bAceptarActionPerformed

    private void jComboCircuitosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboCircuitosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboCircuitosActionPerformed


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ModificarEscuderia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ModificarEscuderia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ModificarEscuderia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ModificarEscuderia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ModificarCarrera().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAceptar;
    private javax.swing.JButton bCancelar;
    private javax.swing.JComboBox jComboCircuitos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField tNombre;
    // End of variables declaration//GEN-END:variables
}
